import Fly from 'flyio'
const fly = new Fly()

fly.config.baseURL = 'https://toutiao.itheima.net/v1_0'
fly.config.timeout = 5000

//添加请求拦截器
fly.interceptors.request.use((request) => {
  //给所有请求添加自定义header
  // request.headers["8f985530-0573-4b30-9d5e-a2e99d1961a3"] = "flyio";
  // request.headers="8f985530-0573-4b30-9d5e-a2e99d1961a3"
  // const token = wx.getStorageSync('token') 
  wx.showLoading({
    title:"加载中"
  })
  const token  = wx.getStorageSync('token')
  request.headers = {
        "Content-Type":"application/x-www-form-urlencoded",
        "Authorization":`Bearer ${token}` 
      }
    // 8ce6fbbb-74b4-4d69-ae2b-9de021f24b71
  // {
  //   'content-type': 'application/json',
  //   'token': wx.getStorageSync('token'),
  // },
 
  //打印出请求体
  console.log(request.body)
  return request;
})

//添加响应拦截器，响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
  (response) => {
    //只将请求结果的data字段返回
    wx.hideLoading()
    return response.data
   
  },
  (err) => {
    //发生网络错误后会走到这里
    //return Promise.resolve("ssss")
  
  }
)

export default fly